﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TPLOAWebService.Models;

namespace TPLOAWebService.Controllers
{
    public class MCsController : ApiController
    {
        static readonly IMCRepository repository = new MCRepository();

        [ActionName("Get")]
        public IEnumerable<MC> GetAllMCs()
        {
            return repository.GetAll();
        }

        public MC GetMC(int id)
        {
            MC item = repository.Get(id);
            if (item == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            return item;
        }

        public HttpResponseMessage PostMC(MC item)
        {
            item = repository.Add(item);
            var response = Request.CreateResponse<MC>(HttpStatusCode.Created, item);

            string uri = Url.Link("DefaultApi", new { id = item.MCID });
            response.Headers.Location = new Uri(uri);
            return response;
        }

        public void PutMC(int id, MC mc)
        {
            mc.MCID = id;
            if (!repository.Update(mc))
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
        }

        public HttpResponseMessage DeleteMC(int id)
        {
            repository.Remove(id);
            return new HttpResponseMessage(HttpStatusCode.NoContent);
        }

        [HttpGet]
        [ActionName("NewMCID")]
        public int NewMCID()
        {
            return TPLOADBManager.GetNewMCID();
        }
    }
}
